// Package models
// @Title  adminUserModel.go
// @Description  管理员模型
// @Author  小斌 (时间 格式是2021/12/02  10:20)
// @Update  小斌 (时间 格式是2021/12/02  10:20)

package models

import (
	"beego-admin/library"
	"github.com/astaxie/beego/orm"
	"time"
)

// AdminUserObj 用户角色 实体类
type AdminUserObj struct {
	Id                 int    `valid:"id"`
	AdminUser               string `valid:"admin_user"`
	PassWord                string `valid:"pass_word"`
	RoleId 					int `valid:"role_id"`
	Gender                int `valid:"gender"`
	Mobile                string `valid:"mobile"`
	Email                string `valid:"email"`
	Remark                string `valid:"remark"`
	IsDelete				int `valid:"is_delete"`
	UpdateTime                time.Time `valid:"update_time" orm:"auto_now"`
	AddTime                time.Time `valid:"add_time" orm:"auto_now_add"`

}

func (this *AdminUserObj) TableName() string {
	return "admin_user"
}

func init() {
	orm.RegisterModel(new(AdminUserObj))
}

// GetAdminUserList 获取分页数据
func GetAdminUserList(pageSize, page int, userName string) (*[]AdminUserObj, int, error) {
	var (
		list []AdminUserObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(AdminUserObj)).OrderBy("-add_time")
	if userName != "" {
		qs = qs.Filter("admin_user", userName)
	}
	_, err = qs.Filter("is_delete",0).Limit(pageSize).Offset((page - 1) * pageSize).RelatedSel().All(&list)
	count, _ := qs.Filter("is_delete",0).Count()
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, 0, err
	}

	return &list, int(count), nil
}

// GetAdminUserInfoByAdminName 根据用户名获取记录
func GetAdminUserInfoByAdminName(adminUser string) (AdminUserObj,error) {
	var (
		row AdminUserObj
		err  error
	)

	err = orm.NewOrm().QueryTable(new(AdminUserObj)).Filter("admin_user", adminUser).Filter("is_delete", 0).One(&row)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return row,nil
	}

	return row,nil
}

// GetAdminUserInfoById 根据用户名获取记录
func GetAdminUserInfoById(id int) (AdminUserObj,error) {
	var (
		row AdminUserObj
		err  error
	)

	err = orm.NewOrm().QueryTable(new(AdminUserObj)).Filter("id", id).Filter("is_delete", 0).One(&row)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return row,nil
	}

	return row,nil
}

// AddAdminUser 添加管理员
func AddAdminUser(userName string,passWord string,roleId int,gender int,mobile string,email string,remark string) int64 {
	password := library.Md5V(passWord)
	adminUser := AdminUserObj{
		AdminUser:    userName,
		RoleId: roleId,
		PassWord:  password,
		Gender: gender,
		Mobile:mobile,
		Email: email,
		Remark: remark,
		IsDelete: 0,
	}

	id,err := orm.NewOrm().Insert(&adminUser)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return id
}

// UpdateAdminUserById 修改管理员
func UpdateAdminUserById(id int,passWord string,roleId int,gender int,mobile string,email string,remark string) int64 {
	adminUser := AdminUserObj{
		Id:    id,
		PassWord:    passWord,
		RoleId: roleId,
		Gender: gender,
		Mobile:mobile,
		Email: email,
		Remark: remark,
	}
	num,err := orm.NewOrm().Update(&adminUser,"gender","role_id","mobile","email","remark","pass_word")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}

// DeleteAdminUserById 删除管理员
func DeleteAdminUserById(id int) int64 {
	adminUser := AdminUserObj{
		Id:    id,
		IsDelete: 1,
	}
	num,err := orm.NewOrm().Update(&adminUser,"is_delete")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}

// GetAdminUserCountByRoleId 根据角色id获取总数量
func GetAdminUserCountByRoleId(roleId int) (int, error) {
	var err error

	qs := orm.NewOrm().QueryTable(new(AdminUserObj)).OrderBy("-add_time")

	count, _ := qs.Filter("role_id",roleId).Filter("is_delete",0).Count()
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0, err
	}

	return int(count), nil
}


